﻿#region <<版本注释>>
/*---------------------------------------------------------------------
* 项目名称：Mac随机生成算法
* 项目描述：
* 类 名 称：MacGenerator
* 类 描 述：
* 所在的域：DESKTOP-6CB1TV7
* 命名空间：Mac随机生成算法
* 机器名称：DESKTOP-6CB1TV7
* CLR 版本：4.0.30319.42000
* 作    者：wky
* 创建时间：2021/12/22 11:32:36
* 更新时间：2021/12/22 11:32:36
* 版 本 号:V1.0.0
***********************************************************************
* Copyright @ wky 2021. All rights reserved.
***********************************************************************
//-------------------------------------------------------------------*/
#endregion

using System;
using System.Collections.Generic;
using System.Text;

namespace Mac随机生成算法
{
    class MacGenerator
    {
        /// <summary>
        /// mac地址分隔符
        /// </summary>
        const string MacSeparator = ":";

        /// <summary>
        /// 获取时间戳
        /// </summary>
        /// <param name="dateTime"></param>
        /// <returns></returns>
        /*public static Int64 GetTimeStamp(DateTime dateTime)
        {
            TimeSpan ts = DateTime.UtcNow - new DateTime(1970, 1, 1, 0, 0, 0, 0);
            return Convert.ToInt64(ts.TotalSeconds);
        }*/

        /// <summary>
        /// 获取今天是周几
        /// </summary>
        /// <returns>0为星期天，1为星期一，...，6为星期六</returns>
        public static int DayOfWeek() {
            return (int)DateTime.Now.DayOfWeek;
        }

        /// <summary>
        /// 计算相对于2020-01-01的周数
        /// </summary>
        /// <returns></returns>
        public static int WeekOfYear() {
            int weeks = 0;
            TimeSpan ts = DateTime.Now - Convert.ToDateTime("2020-01-01");
            weeks = (int)(ts.TotalDays / 7);
            return weeks;
        }


        static int GetRandomSeed()
        {
            byte[] bytes = new byte[4];
            System.Security.Cryptography.RNGCryptoServiceProvider rng = new System.Security.Cryptography.RNGCryptoServiceProvider();
            rng.GetBytes(bytes);
            return BitConverter.ToInt32(bytes, 0);
        }

        /// <summary>
        /// 获取随机数
        /// </summary>
        /// <returns></returns>
        public static int GetRandom(int minVal,int maxVal) {
            Random rd = new Random(GetRandomSeed());
            int iRd = rd.Next(minVal, maxVal);
            return iRd;
        }

        /// <summary>
        /// 获取weeks的最终转化结果
        /// 先根据iRd进行移位，高位移进左边，再根据iRd%2结果是否为非0判断是否需要位是否需要交换
        /// 位交换先不做
        /// </summary>
        /// <param name="weeks"></param>
        /// <param name="iRd"></param>
        /// <returns></returns>
        static public int GetWeeksResult(int weeks,int iRd) {
            
            int bit9  = (weeks & (0x00000001 <<  9)) > 0 ? 1 : 0;
            int bit10 = (weeks & (0x00000001 << 10)) > 0 ? 1 : 0;
            int bit11 = (weeks & (0x00000001 << 11)) > 0 ? 1 : 0;

            int w = weeks;
            if (iRd == 1) {
                w = ((weeks << iRd) + bit11 ) & 0x000007FF;
            } else if (iRd == 2) {
                w = ((weeks << iRd) + bit11 << 2 + bit10) & 0x000007FF;
            } else if (iRd == 3) {
                w = ((weeks << iRd) + bit11 << 2 + bit10 << 1 + bit9) & 0x000007FF;
            } else {

            }

            if ((iRd % 2) > 0) {
                //暂时不做位交换
            }

            return w;
        }

        /// <summary>
        /// 第一与第六字节作为周数
        /// xx  xxx xxxxxxxx xxx
        /// xx作为随机位，取值0-3，即为周数需要移右几位，高位补在左边
        /// xxx xxxxxxxx 低11位 作为周数，以2020年第一周为起始周
        /// 最后一个xxx作为一周的星期几，0为周天，6为周六
        /// </summary>
        /// <returns></returns>
        static public byte[] Generatorb1b6() {

            byte[] bytes = new byte[2];

            int iRd = GetRandom(0, 4);
            int dayOfWeek = DayOfWeek();
            int weeks = WeekOfYear();

            int bit14_15 = (iRd << 14);
            int bit3_13 = (GetWeeksResult(weeks, iRd) << 3);
            int bit0_2 = dayOfWeek;

            int b1b6 =  bit14_15 + bit3_13 + bit0_2;

            /*UInt16 b1b6 = (UInt16)GetRandom(0, 65536);*/
            bytes[0] = (byte)(b1b6 >> 8);
            bytes[1] = (byte)b1b6;

            return bytes;
        }

        static public byte[] Generatorb2b3b4b5() {

            byte[] bytes = new byte[4];

            /*bytes[0] = (byte)GetRandom(0, 255);
            bytes[1] = (byte)GetRandom(0, 255);
            bytes[2] = (byte)GetRandom(0, 255);
            bytes[3] = (byte)GetRandom(0, 255);*/

            /*int num = GetRandom(0, 65535);
            bytes[0] = (byte)(num >> 8);
            bytes[1] = (byte)num;
            num = GetRandom(0, 65535);
            bytes[2] = (byte)(num >> 8);
            bytes[3] = (byte)num;*/

            /*int num = GetRandom(0, 16777216);
            bytes[0] = (byte)(num >> 16);
            bytes[1] = (byte)(num >> 8);
            bytes[2] = (byte)(num);
            bytes[3] = (byte)GetRandom(0, 256);*/

            int num = GetRandom(0, 2147483647);
            bytes[0] = (byte)(num >> 24);
            bytes[1] = (byte)(num >> 16);
            bytes[2] = (byte)(num >> 8);
            bytes[3] = (byte)(num);

            return bytes;
        }

        static public string GeneratorMacString() {

            string macStr = "";

            const int b1Index = 0;
            const int b6Index = 1;

            const int b2Index = 0;
            const int b3Index = 1;
            const int b4Index = 2;
            const int b5Index = 3;

            byte[] b1b6 = Generatorb1b6();
            byte[] b2b3b4b5 = Generatorb2b3b4b5();

            macStr = b1b6[b1Index].ToString("X2") + MacSeparator + b2b3b4b5[b2Index].ToString("X2") + MacSeparator 
                + b2b3b4b5[b3Index].ToString("X2") + MacSeparator + b2b3b4b5[b4Index].ToString("X2") + MacSeparator
                + b2b3b4b5[b5Index].ToString("X2") + MacSeparator + b1b6[b6Index].ToString("X2");

           /* macStr = b2b3b4b5[b2Index].ToString("X2") + MacSeparator
                + b2b3b4b5[b3Index].ToString("X2") + MacSeparator + b2b3b4b5[b4Index].ToString("X2") + MacSeparator
                + b2b3b4b5[b5Index].ToString("X2") ;*/

            return macStr;
        }

        static public byte[] GeneratorMacBytes() {
            byte[] macBytes = new byte[6];

            return macBytes;
        }

        /// <summary>
        /// 根据MAC字节数组返回生产日期
        /// </summary>
        /// <param name="macBytes"></param>
        /// <returns></returns>
        static public DateTime DateProduction(byte[] macBytes) {
            DateTime dt = DateTime.Now;

            return dt;
        }

        /// <summary>
        /// 根据MAC字符串返回生产日期
        /// </summary>
        /// <param name="macString"></param>
        /// <returns></returns>
        static public DateTime DateProduction(string macString) {
            DateTime dt = DateTime.Now;

            return dt;
        }
    }
}
